﻿using System.Linq;
using Microsoft.Xna.Framework;

namespace Babylon.Primitives
{
    /// <summary>
    /// Colored cube primitive
    /// </summary>
    public class ColoredCube : CubeBase<VertexPositionNormalColor>
    {
        /// <summary>
        /// creates an instance of ColoredCube
        /// </summary>
        /// <param name="width">width of the cube</param>
        /// <param name="height">height of the cube</param>
        /// <param name="depth">depth of the cube</param>
        /// <param name="color">uniform color</param>
        public ColoredCube(float width, float height, float depth, Color color)
            : this(width, height, depth, color, color,color, color, color, color, color, color)
        {
        }

        Color[] _colors = new Color[8];

        /// <summary>
        /// creates an instance of ColoredCube
        /// </summary>
        /// <param name="width">width of the cube</param>
        /// <param name="height">height of the cube</param>
        /// <param name="depth">depth of the cube</param>
        /// <param name="color0">color of point 0</param>
        /// <param name="color1">color of point 1</param>
        /// <param name="color2">color of point 2</param>
        /// <param name="color3">color of point 3</param>
        /// <param name="color4">color of point 4</param>
        /// <param name="color5">color of point 5</param>
        /// <param name="color6">color of point 6</param>
        /// <param name="color7">color of point 7</param>
        public ColoredCube(float width, float height, float depth, Color color0, Color color1, Color color2, Color color3, Color color4, Color color5, Color color6, Color color7)
            :base(width, height, depth)
        {
            _colors[0] = color0;
            _colors[1] = color1;
            _colors[2] = color2;
            _colors[3] = color3;
            _colors[4] = color4;
            _colors[5] = color5;
            _colors[6] = color6;
            _colors[7] = color7;
        }
        protected override VertexPositionNormalColor CreateVertex(Microsoft.Xna.Framework.Vector3 position, Microsoft.Xna.Framework.Vector3 normal)
        {
            return new VertexPositionNormalColor { Position = position, Normal = normal };
        }

        protected override Microsoft.Xna.Framework.Graphics.VertexDeclaration GetVertexDeclaration()
        {
            return VertexPositionNormalColor.GetVertexDeclaration();
        }

        protected override void BeforeCreatingVertexArray()
        {
        }

        protected override void AfterCreatingVertexArray(VertexPositionNormalColor[] vertices)
        {
            var positionColors = vertices.Select((v) => v.Position).Distinct()
                .Select((p,ix)=>new {Position = p, Index = ix})
                .ToDictionary(p=>p.Position, p=>_colors[p.Index % _colors.Length]);

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].Color = positionColors[vertices[i].Position];
            }

        }
    }
}
